﻿using Microsoft.AspNetCore.Mvc;
using RuoVea.GoFastdfs.Common;
using RuoVea.GoFastdfs.Service;
using RuoVea.GoFastdfs.Service.Inserface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RuoVea.GoFastdfs.Web.Controllers
{
    /// <summary>
    /// 文件列表相关controller
    /// </summary>
  
    //[Route("Filetdo/{Controller}")]
    public class FiletdoController : BaseController
    {
        private IFileService _fileService;

        public FiletdoController(IFileService fileService)
        {
            _fileService = fileService;
        }

        /// <summary>
        /// 文件列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult upload()
        {
            return View();
        }

        /// <summary>
        /// 获取一级目录
        /// </summary>
        /// <returns></returns>
        public IActionResult getParentFile()
        {
           var  t= _fileService.getParentFile("group1", "http://127.0.0.1:8080/");
           //var  t= _fileService.getParentFile(getPeersGroupName(), getPeersUrl());

            return Content(ResponseBean.success(t).ToJsonString());
        }

        //    /**
        //     * @description 指定目录获取
        //     * @param dir  dir
        //     * @return com.perfree.common.ResponseBean
        //     * @author Perfree
        //     */
        //    @RequestMapping("/file/getDirFile")
        //@ResponseBody
        //public ResponseBean getDirFile(String dir)
        //    {
        //        return ResponseBean.success(fileService.getDirFile(getShowUrl(), getPeersUrl(), dir));
        //    }

        //    /**
        //     * @description 删除文件
        //     * @param md5  md5
        //     * @return com.perfree.common.ResponseBean
        //     * @author Perfree
        //     */
        //    @RequestMapping("/file/deleteFile")
        //@ResponseBody
        //public ResponseBean deleteFile(String md5)
        //    {
        //        if (fileService.deleteFile(getPeersUrl(), md5))
        //        {
        //            return ResponseBean.success();
        //        }
        //        return ResponseBean.fail("删除失败");
        //    }

        //    /**
        //     * @description 文件信息
        //     * @param md5  md5
        //     * @return com.perfree.common.ResponseBean
        //     * @author Perfree
        //     */
        //    @RequestMapping("/file/details")
        //@ResponseBody
        //public ResponseBean details(String md5)
        //    {
        //        return fileService.details(getPeersUrl(), md5);
        //    }

        //    /**
        //     * @description 下载文件
        //     * @param path path
        //     * @param name name
        //     * @param request request
        //     * @param response response
        //     * @author Perfree
        //     */
        //    @RequestMapping("/file/downloadFile")
        //@ResponseBody
        //public void downloadFile(String path, String name, HttpServletRequest request, HttpServletResponse response)
        //    {
        //        response.setHeader("Content-Disposition", "attachment;filename=" + name);
        //        response.setContentType("application/octet-stream");
        //        BufferedInputStream in = null;
        //        try
        //        {
        //            URL url = new URL(getPeersUrl() + "/" + path + "/" + name);
        //        in = new BufferedInputStream(url.openStream());
        //            response.reset();
        //            response.setContentType("application/octet-stream");
        //            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name, "UTF-8"));
        //            // 将网络输入流转换为输出流
        //            int i;
        //            while ((i = in.read()) != -1) {
        //                response.getOutputStream().write(i);
        //            }
        //            response.getOutputStream().close();
        //        }
        //        catch (IOException e)
        //        {
        //            e.printStackTrace();
        //        }
        //        finally
        //        {
        //            try
        //            {
        //                if (in != null){
        //               in.close();
        //                }
        //            }
        //            catch (IOException e)
        //            {
        //                e.printStackTrace();
        //            }
        //        }
        //    }

    }
}
